<template>
  <div class="attractions-container">
    <div class="page-header">
      <h1>{{ pageTitle }}</h1>
      <div class="filter-section">
        <el-select v-model="selectedCategory" placeholder="选择景点分类" clearable @change="handleCategoryChange">
          <el-option
            v-for="category in categories"
            :key="category.categoryId"
            :label="category.categoryName"
            :value="category.categoryId"
          />
        </el-select>
        <el-input
          v-model="searchKeyword"
          placeholder="搜索景点名称"
          clearable
          @clear="fetchAttractions"
        >
          <template #prefix>
            <el-icon><Search /></el-icon>
          </template>
        </el-input>
        <el-button type="primary" @click="fetchAttractions">搜索</el-button>
      </div>
    </div>
    
    <el-row :gutter="20" class="attractions-list" v-loading="loading">
      <el-col :xs="24" :sm="12" :md="8" :lg="6" v-for="attraction in attractions" :key="attraction.attractionId">
        <el-card class="attraction-card" shadow="hover" @click="viewDetail(attraction.attractionId)">
          <div class="attraction-image" :style="{ backgroundImage: `url(${attraction.coverImages && attraction.coverImages.length > 0 ? attraction.coverImages[0] : '../assets/placeholder.jpg'})` }">
            <div class="attraction-rating">
              <el-rate v-model="attraction.avgRating" disabled text-color="#ff9900" />
              <span>{{ attraction.avgRating }}分</span>
            </div>
          </div>
          <div class="attraction-info">
            <h3>{{ attraction.name }}</h3>
            <div class="attraction-tags" v-if="attraction.tags && attraction.tags.length > 0">
              <el-tag v-for="(tag, index) in attraction.tags" :key="index" size="small">
                {{ tag }}
              </el-tag>
            </div>
            <p class="attraction-desc">{{ attraction.description ? (attraction.description.substring(0, 60) + '...') : '暂无描述' }}</p>
            <div class="attraction-footer">
              <span class="price">
                <el-icon><Money /></el-icon>
                ¥{{ attraction.ticketPrice || '免费' }}
              </span>
              <span class="view-count">
                <el-icon><View /></el-icon>
                {{ attraction.viewCount || 0 }}
              </span>
            </div>
          </div>
        </el-card>
      </el-col>
    </el-row>
    
    <div v-if="attractions.length === 0 && !loading" class="empty-state">
      <el-empty description="暂无符合条件的景点" />
    </div>
    
    <div class="pagination-container" v-if="total > 0">
      <el-pagination
        v-model:current-page="currentPage"
        v-model:page-size="pageSize"
        :page-sizes="[12, 24, 36, 48]"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted, computed } from 'vue';
import { useRouter, useRoute } from 'vue-router';
import { Search, Money, View } from '@element-plus/icons-vue';
import api from '@/api/auth';
import { getAllCategories } from '@/api/category';

const router = useRouter();
const route = useRoute();

// 状态变量
const loading = ref(false);
const attractions = ref([]);
const categories = ref([]);
const selectedCategory = ref('');
const searchKeyword = ref('');
const currentPage = ref(1);
const pageSize = ref(12);
const total = ref(0);

// 计算页面标题
const pageTitle = computed(() => {
  if (selectedCategory.value) {
    const category = categories.value.find(c => c.categoryId === selectedCategory.value);
    return category ? `${category.categoryName}景点` : '景点浏览';
  }
  return '景点浏览';
});

// 获取景点列表
const fetchAttractions = async () => {
  loading.value = true;
  try {
    const params = {
      pageNum: currentPage.value,
      pageSize: pageSize.value,
      keyword: searchKeyword.value || undefined,
      categoryId: selectedCategory.value || undefined
    };
    
    const response = await api.get('/attraction/list', { params });
    
    if (response.code === 200) {
      attractions.value = response.data.list || [];
      total.value = response.data.total || 0;
    } else {
      console.error('获取景点列表失败:', response.message);
    }
  } catch (error) {
    console.error('获取景点列表出错:', error);
  } finally {
    loading.value = false;
  }
};

// 获取所有分类
const fetchCategories = async () => {
  try {
    const response = await getAllCategories();
    if (response.code === 200) {
      categories.value = response.data || [];
    }
  } catch (error) {
    console.error('获取分类列表失败:', error);
  }
};

// 查看景点详情
const viewDetail = (id) => {
  router.push(`/attraction/${id}`);
};

// 处理分类变化
const handleCategoryChange = () => {
  currentPage.value = 1;
  fetchAttractions();
};

// 处理分页大小变化
const handleSizeChange = (size) => {
  pageSize.value = size;
  fetchAttractions();
};

// 处理页码变化
const handleCurrentChange = (page) => {
  currentPage.value = page;
  fetchAttractions();
};

// 页面加载时
onMounted(() => {
  // 从路由参数中获取分类ID
  if (route.query.categoryId) {
    selectedCategory.value = Number(route.query.categoryId);
  }
  
  // 获取分类列表
  fetchCategories();
  
  // 获取景点列表
  fetchAttractions();
});
</script>

<style scoped>
.attractions-container {
  max-width: 1200px;
  margin: 20px auto;
  padding: 20px;
  border-radius: 10px;
  box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
  position: relative;
  overflow: hidden;
}

/* 添加背景图片 */
.attractions-container::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-image: url('@/assets/bg.jpg');
  background-size: cover;
  background-position: center;
  opacity: 0.15; /* 降低背景图片不透明度，使内容更清晰 */
  z-index: -1;
}

/* 添加渐变叠加层，增强可读性 */
.attractions-container::after {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: linear-gradient(135deg, rgba(245, 247, 250, 0.9) 0%, rgba(228, 236, 247, 0.9) 100%);
  z-index: -1;
}

.page-header {
  margin-bottom: 30px;
  text-align: center;
}

.page-header h1 {
  margin: 0 0 20px 0;
  font-size: 32px;
  color: #303133;
  font-weight: 600;
  position: relative;
  display: inline-block;
  padding-bottom: 10px;
}

.page-header h1::after {
  content: '';
  position: absolute;
  bottom: 0;
  left: 50%;
  transform: translateX(-50%);
  width: 60px;
  height: 3px;
  background: linear-gradient(90deg, #409EFF, #67C23A);
  border-radius: 3px;
}

.filter-section {
  display: flex;
  gap: 15px;
  margin: 30px auto;
  flex-wrap: wrap;
  justify-content: center;
  max-width: 800px;
  background-color: white;
  padding: 20px;
  border-radius: 8px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

.filter-section .el-input,
.filter-section .el-select {
  min-width: 200px;
}

.attractions-list {
  margin-top: 30px;
}

.attraction-card {
  margin-bottom: 25px;
  transition: all 0.3s;
  cursor: pointer;
  height: 100%;
  border: none;
  border-radius: 12px;
  overflow: hidden;
}

.attraction-card:hover {
  transform: translateY(-8px);
  box-shadow: 0 8px 20px rgba(0, 0, 0, 0.15);
}

.attraction-image {
  height: 200px;
  background-size: cover;
  background-position: center;
  position: relative;
  background-color: #f5f7fa;
  transition: all 0.5s;
}

.attraction-card:hover .attraction-image {
  transform: scale(1.05);
}

.attraction-rating {
  position: absolute;
  top: 15px;
  right: 15px;
  background-color: rgba(0, 0, 0, 0.7);
  padding: 5px 12px;
  border-radius: 20px;
  color: white;
  display: flex;
  align-items: center;
  gap: 8px;
  backdrop-filter: blur(5px);
}

.attraction-info {
  padding: 20px;
  background-color: white;
}

.attraction-info h3 {
  margin: 0 0 12px;
  font-size: 18px;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  color: #303133;
}

.attraction-tags {
  margin-bottom: 12px;
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

.attraction-tags .el-tag {
  border-radius: 15px;
  padding: 0 10px;
  height: 24px;
  line-height: 22px;
}

.attraction-desc {
  color: #606266;
  margin-bottom: 15px;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
  font-size: 14px;
  line-height: 1.6;
}

.attraction-footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 14px;
  padding-top: 10px;
  border-top: 1px solid #f0f2f5;
}

.price {
  color: #F56C6C;
  font-weight: bold;
  display: flex;
  align-items: center;
  gap: 5px;
  font-size: 16px;
}

.view-count {
  color: #909399;
  display: flex;
  align-items: center;
  gap: 5px;
}

.empty-state {
  padding: 60px 0;
  background-color: white;
  border-radius: 8px;
  margin-top: 20px;
}

.pagination-container {
  margin-top: 40px;
  display: flex;
  justify-content: center;
  padding: 20px;
  background-color: white;
  border-radius: 8px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.05);
}

/* 添加响应式设计 */
@media (max-width: 768px) {
  .filter-section {
    flex-direction: column;
    padding: 15px;
  }
  
  .page-header h1 {
    font-size: 24px;
  }
  
  .attractions-container {
    padding: 15px;
  }
  
  .attraction-image {
    height: 180px;
  }
}

/* 添加一些动画效果 */
@keyframes fadeIn {
  from {
    opacity: 0;
    transform: translateY(20px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.attractions-list {
  animation: fadeIn 0.5s ease-out;
}

/* 为分页器添加样式 */
:deep(.el-pagination) {
  font-weight: normal;
}

:deep(.el-pagination .el-pagination__total) {
  font-weight: normal;
}

:deep(.el-pagination .el-select .el-input) {
  width: 120px;
}

:deep(.el-pagination .el-pagination__jump) {
  margin-left: 15px;
}
</style> 